package com.EM1.www.servlet;

import com.EM1.www.dao.ConnectPaper;
import com.EM1.www.dao.ConnectQuestion;
import com.EM1.www.po.Paper;
import com.EM1.www.po.Question;
import com.EM1.www.po.Teacher;
import com.EM1.www.service.Checkquestion;
import com.EM1.www.util.UpdateSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/AddQuestion")
public class AddQuestion extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String question=request.getParameter("question");
        String answer=request.getParameter("answer");
        Checkquestion checkquestion=new Checkquestion();
        String warn = checkquestion.checkquestion(question, answer);
        if(warn!=null){
            request.setAttribute("warn",warn);
            request.setAttribute("question",question);
            request.setAttribute("answer",answer);
            request.getRequestDispatcher("addquestion.jsp").forward(request,response);
        }else {
            HttpSession session = request.getSession();
            String parse=request.getParameter("parse");
            String add=request.getParameter("add");
            Teacher teacher = (Teacher) session.getAttribute("teacher");
            ConnectQuestion connectQuestion=new ConnectQuestion();
            Paper paper = (Paper) session.getAttribute("paper");
            try {
                connectQuestion.insertquestion(question,answer,parse, teacher.getId());
                if(paper!=null){
                    ConnectPaper connectPaper=new ConnectPaper();
                    connectPaper.insertpaper(teacher.getId(),paper.getTitle(),paper.getQuestionId());
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            if(add.equals("完成添加")){
                try {
                    UpdateSession.updateQuestion(request);
                    UpdateSession.updatePapers(request);
                    request.getRequestDispatcher("questionbank.jsp").forward(request,response);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }else {
                if(paper!=null){
                    try {
                        ConnectPaper connectPaper=new ConnectPaper();
                        Paper newPaperpaper = connectPaper.selectnewPaper(paper);
                        ArrayList<Question> questions = connectQuestion.selectQuestion(newPaperpaper.getQuestionId());
                        request.setAttribute("questions",questions);
                        session.setAttribute("paper",newPaperpaper);
                        request.getRequestDispatcher("changepaper.jsp").forward(request,response);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }else {
                    request.getRequestDispatcher("addquestion.jsp").forward(request,response);
                }
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
